package com.gdut.simple.model.impl;

import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.data.entity.Favorites;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IFavoritesModel;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class FavoritesModelImpl implements IFavoritesModel {

    @Override
    public void loadData(final User user, final List<String> types, final RequestCallback<List<List<Favorites>>> callback) {
        LogUtils.i("FavoritesModelImpl loadData user objectId is " + user.getObjectId());
        BmobQuery<Favorites> queryType = new BmobQuery<>();
        queryType.addWhereEqualTo("user", user);
        queryType.groupby(new String[]{"type"});
        queryType.findStatistics(Favorites.class, new QueryListener<JSONArray>() {
            @Override
            public void done(JSONArray jsonArray, BmobException e) {
                LogUtils.d("findStatistics:done");
                if (e == null && jsonArray != null) {
                    LogUtils.d("findStatistics:e == null && jsonArray != null");
                    types.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        try {
                            types.add(jsonArray.getJSONObject(i).getString("type"));
                        } catch (JSONException je) {
                            if (callback != null) {
                                callback.onFailure(je.getMessage());
                            }
                        }
                    }
                    BmobQuery<Favorites> queryList = new BmobQuery<>();
                    queryList.addWhereEqualTo("user", user);
                    queryList.findObjects(new FindListener<Favorites>() {
                        @Override
                        public void done(List<Favorites> list, BmobException e) {
                            LogUtils.d("findObjects:done");
                            if (e == null && list != null) {
                                LogUtils.d("findObjects:e == null && list != null");
                                final List<List<Favorites>> data = new ArrayList<>();
                                for (int i = 0; i < types.size(); i++) {
                                    List<Favorites> child = new ArrayList<>();
                                    for (int j = 0; j < list.size(); j++) {
                                        if (list.get(j).getType().equals(types.get(i))) {
                                            child.add(list.get(j));
                                        }
                                    }
                                    data.add(child);
                                }
                                if (callback != null) {
                                    callback.onSuccess(data);
                                }
                            } else {
                                LogUtils.d("findObjects:e != null || list == null");
                                if (callback != null) {
                                    if (e != null) {
                                        callback.onFailure(e.getMessage());
                                    } else {
                                        callback.onFailure("query list return null");
                                    }
                                }
                            }
                        }
                    });
                } else {
                    LogUtils.e("findStatistics:e != null || jsonArray == null");
                    if (callback != null) {
                        if (e != null) {
                            callback.onFailure(e.getMessage());
                        }
                    }
                }
            }
        });
    }

}
